package com.xms.form.service;

import com.xms.form.core.FormMetaDescriptor;
import com.xms.form.core.component.*;
import com.xms.form.entity.SystemForm;
import com.xms.schema.entity.Attribute;
import com.xms.schema.service.attribute.IAttributeUnionFinderService;
import com.xms.utils.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * <p>
 * FormService
 * </p>
 *
 * @author migo
 * @since 2021/9/9
 */
@Service
public class FormBuilderService implements IFormBuilderService {
    private IAttributeUnionFinderService _attributeUnionFinderService;

    @Autowired
    public FormBuilderService(IAttributeUnionFinderService attributeUnionFinderService)
    {
        _attributeUnionFinderService = attributeUnionFinderService;
    }

    @Override
    public FormMetaDescriptor build(SystemForm formEntity) {
        FormMetaDescriptor result = new FormMetaDescriptor();
        result.form = JsonUtil.toBean(formEntity.getFormconfig(), FormDescriptor.class);
        UUID entityid = formEntity.getEntityid();
        List<Attribute> attributeMetadatas = _attributeUnionFinderService.getByEntityId(entityid);
        List<String> attributeNames = new ArrayList<>();
        if (result.form.Header != null && result.form.Header.Rows != null)
        {
            //header
            for (RowDescriptor row : result.form.Header.Rows)
            {
                for (CellDescriptor cell : row.Cells)
                {
                    if (cell.Control.getControlType().val() == FormControlType.Standard.val()
                            || cell.Control.getControlType().val() == FormControlType.Hidden.val()
                            || cell.Control.getControlType().val() == FormControlType.Lookup.val()
                            || cell.Control.getControlType().val() == FormControlType.OptionSet.val())
                    {
                        attributeNames.add(cell.Control.Name.toLowerCase());
                        cell.Control.AttributeMetadata = attributeMetadatas.stream().filter(n -> n.getName().equalsIgnoreCase(cell.Control.Name)).findFirst().orElse(null);
                    }
                }
            }
        }
        if (result.form.Panels != null)
        {
            //tabs
            for (PanelDescriptor tab : result.form.Panels)
            {
                for (SectionDescriptor sec : tab.Sections)
                {
                    for (RowDescriptor row : sec.Rows)
                    {
                        for (CellDescriptor cell : row.Cells)
                        {
                            if (cell.Control.getControlType().val() == FormControlType.Standard.val()
                                    || cell.Control.getControlType().val() == FormControlType.Hidden.val()
                                    || cell.Control.getControlType().val() == FormControlType.Lookup.val()
                                    || cell.Control.getControlType().val() == FormControlType.OptionSet.val())
                            {
                                attributeNames.add(cell.Control.Name.toLowerCase());
                                cell.Control.AttributeMetadata = attributeMetadatas.stream().filter(n -> n.getName().equalsIgnoreCase(cell.Control.Name)).findFirst().orElse(null);
                            }
                        }
                    }
                }
            }
        }
        if (result.form.Footer != null && result.form.Footer.Rows != null)
        {
            //footer
            for (RowDescriptor row : result.form.Footer.Rows)
            {
                for (CellDescriptor cell : row.Cells)
                {
                    if (cell.Control.getControlType().val() == FormControlType.Standard.val()
                            || cell.Control.getControlType().val() == FormControlType.Hidden.val()
                            || cell.Control.getControlType().val() == FormControlType.Lookup.val()
                            || cell.Control.getControlType().val() == FormControlType.OptionSet.val())
                    {
                        attributeNames.add(cell.Control.Name.toLowerCase());
                        cell.Control.AttributeMetadata = attributeMetadatas.stream().filter(n -> n.getName().equalsIgnoreCase(cell.Control.Name)).findFirst().orElse(null);
                    }
                }
            }
        }
        result.attributes = attributeMetadatas.stream().filter(n -> attributeNames.contains(n.getName())).collect(Collectors.toList());
        return result;
    }
}
